<?php
/**
 * ClassName:
 * @auditor:leijunxiang
 * @time 2025/4/3 15:00
 */

namespace app\modelOperate\freightForward;

use app\modelOperate\ModelOpeBase;
use think\facade\Log;

class WarehousePriceOpe extends ModelOpeBase
{
    protected $pk = 'warehouseId';

    /**
     * 保存数据
     * @param $data
     * @return void
     */
    public function saveData($data)
    {
        $db = $this->getDb();
        $flag = $db->insert($data);
        return $flag;
    }

    /**
     * 根据详情id查询数据
     */
    public function selectDataByDetailId($detailId)
    {
        $db = $this->getDb();
        $data = $db
            ->where('detailId', '=', $detailId)
            ->order('time', 'desc')
            ->select()
            ->toArray();
        return $data;
    }

    /**
     * 根据详情id和重量查询数据(详情id,重量名称)
     */
    public function selectDataByDetailIdAndWeightName($detailId, $weightName)
    {
        $db = $this->getDb();
        $data = $db
            ->where('detailId', '=', $detailId)
            ->where('weightName', '=', $weightName)
            ->order('time', 'desc')
            ->select()
            ->toArray();
        return $data;
    }

    /**
     * 根据详情id和重量更新价格
     * @return string
     */
    public function updatePriceByDetailIdAndWeight($detailId, $weight, $price,$time)
    {
        $db = $this->getDb();
        $num = $db->where('detailId', '=', $detailId)->where('weightName', '=', $weight)->update(['price' => $price, 'time' => $time]);
        return $num;
    }

    /**
     * 查询仓库id
     * @param $detailId
     * @param $weight
     * @return mixed
     */
    public function selectWarehouseIdByDetailIdAndWeight($detailId, $weight)
    {
        $db = $this->getDb();
        $warehouseId = $db->where('detailId', '=', $detailId)->where('weightName', '=', $weight)->field('warehouseId')->find()['warehouseId'];
        return $warehouseId;
    }

    /**
     * 修改
     */
    public function updatePriceData($data)
    {
        $db = $this->getDb();
        $flag = $db->where('warehouseId',$data['warehouseId'])->update(['price'=>$data['price']]);
        return $flag;
    }
}